
using System.ServiceModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using AdventureWorks.Client.Objects;
using AdventureWorks.Client.Silverlight.EmployeeServiceRef;
using Xomega.Framework;

namespace AdventureWorks.Client.Silverlight
{
    public partial class EmployeeDetails : Page
    {
        private EmployeeObject obj;

        public EmployeeDetails()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            obj = new EmployeeObject();
            pnlMain.DataContext = obj;
            gridPayHistory.ItemsSource = obj.PayHistoryObjectList;
            Load();
        }

        protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
        {
            bool? modified = obj.IsModified();
            if (modified.HasValue && modified.Value && MessageBox.Show(
                "You have unsaved changes. Do you want to discard them and navigate away?",
                "Unsaved Changes", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
                e.Cancel = true;
        }

        private void Load()
        {
            int kEmployeeId;
            if (!int.TryParse(NavigationContext.QueryString["EmployeeId"], out kEmployeeId)) return;
            EmployeeServiceClient cltEmployee = new EmployeeServiceClient();
            obj.EmployeeIdProperty.Value = kEmployeeId;
            cltEmployee.ReadCompleted += delegate(object sender, ReadCompletedEventArgs e)
            {
                FaultException<ErrorList> fex = e.Error as FaultException<ErrorList>;
                if (fex != null && fex.Detail != null)
                    MessageBox.Show(fex.Detail.ErrorsText, "Service Errors", MessageBoxButton.OK);
                else obj.FromDataContract(e.Result);
            };
            cltEmployee.ReadAsync(new EmployeeKey{EmployeeId = kEmployeeId});
            cltEmployee.PayHistory_ReadListCompleted += delegate(object sender, PayHistory_ReadListCompletedEventArgs e)
            {
                FaultException<ErrorList> fex = e.Error as FaultException<ErrorList>;
                if (fex != null && fex.Detail != null)
                    MessageBox.Show(fex.Detail.ErrorsText, "Service Errors", MessageBoxButton.OK);
                else obj.PayHistoryObjectList.FromDataContract(e.Result);
            };
            cltEmployee.PayHistory_ReadListAsync(new EmployeeKey{EmployeeId = kEmployeeId});
            cltEmployee.CloseAsync();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            obj.Validate(true);
            ErrorList valErr = obj.GetValidationErrors();
            if (valErr.HasErrors())
            {
                MessageBox.Show(valErr.ErrorsText, "Validation Errors", MessageBoxButton.OK);
                return;
            }

            EmployeeServiceClient cltEmployee = new EmployeeServiceClient();
            ClientSessionManager.Register(cltEmployee.InnerChannel);
            cltEmployee.SaveChangesCompleted += delegate(object s, SaveChangesCompletedEventArgs args)
            {
                cltEmployee.EndSessionAsync();
                cltEmployee.CloseAsync();
                FaultException<ErrorList> fex = args.Error as FaultException<ErrorList>;
                if (fex != null && fex.Detail != null)
                    MessageBox.Show(fex.Detail.ErrorsText, "Service Errors", MessageBoxButton.OK);
                else Load();
            };
            cltEmployee.UpdateCompleted += delegate(object s, AsyncCompletedEventArgs args)
            {
                cltEmployee.SaveChangesAsync(true);
            };
            Employee_UpdateInput inUpdate = new Employee_UpdateInput();
            obj.ToDataContract(inUpdate);
            cltEmployee.UpdateAsync(inUpdate);

            obj.SetModified(false, true);
        }
    }
}
